﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medianamik.UI.Mediasend.Logic;
using Medianamik.Globalization;
using System.Text.RegularExpressions;

namespace Medianamik.UI.Mediasend.UserControls
{
    public partial class ucSending : MediasendUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Data.OnSent += new MailingDataShell.Sent(Data_OnSent);
        }

        void Data_OnSent()
        {
            cmdConfirmanceTesting.Enabled = false;
            cmdSend.Enabled = false;
        }

        #region IMediasendUserControl Members

        public override bool Save()
        {
            //Aucune sauvegarde nécessaire dans cette étape
            return true;
        }


        public override void SetControls()
        {
            lblSentResult.Text = "";
            lblError.Text = "";
        }
        #endregion

        protected void cmdConfirmanceTesting_Click(object sender, EventArgs e)
        {
            bool hasError = false;

            cvTestEmail.IsValid = txtTestingFrom.Text.Trim() != "" && IsValidEmail();
            
            lblError.Text = "";
            lblSentResult.Text = "";

            if (txtTestingFrom.Text.Trim() != "" && IsValidEmail())
            {
                lblSentResult.Text = "";
                if (Data.Save())
                {
                    if (!Data.SendTest(txtTestingFrom.Text))
                        hasError = true;
                }
                else
                    hasError = true;

                if (hasError)
                    lblError.Text = Translator.Translate("Medianamik_Mediasend", "ErrorPleaseContactSupport");
                else
                    lblSentResult.Text = Translator.Translate("Medianamik_Mediasend", "TestSentSucceed");
            }
            else
                cvTestEmail.IsValid = false;

        }

        protected void cmdSend_Click(object sender, EventArgs e)
        {
            lblSentResult.Text = "";
            lblError.Text = "";
            
            bool hasError = false;

            if (Data.Save())
            {
                if (!Data.SendNewsletter())
                    hasError = true;
            }
            else
                hasError = true;

            if (hasError)
                lblError.Text = Translator.Translate("Medianamik_Mediasend", "ErrorPleaseContactSupport");
            else
                lblSentResult.Text = Translator.Translate("Medianamik_Mediasend", "SentSucceed");

        }
        
        /// <summary>
        /// Validation du courriel de test saisie.
        /// </summary>
        /// <returns></returns>
        private bool IsValidEmail()
        {
            var reg = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
            return reg.IsMatch(txtTestingFrom.Text);
        }
    }
}